package com.itheima.spring.bestpractive.controller.evaluator;

import com.itheima.spring.bestpractive.common.myResult.R;
import com.itheima.spring.bestpractive.pojo.*;
import com.itheima.spring.bestpractive.service.AssessmentService;
import com.itheima.spring.bestpractive.vo.req.DisputesReqVo;
import com.itheima.spring.bestpractive.vo.req.assessment.AssessmentIndicatorReqEvaVo;
import com.itheima.spring.bestpractive.vo.resp.assessment.AssessmentIndicatorRespVo;
import com.itheima.spring.bestpractive.vo.resp.employee.EmpEvaRespVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @version 1.0
 * @author: HZY
 * @date: 2025/6/25 17:14
 * @Content:
 */
@RestController
@RequestMapping("/evaluator")
@Tag(name = "考核人员")
public class EvaluatorController {
    @Autowired
    private AssessmentService assessmentService;


    @GetMapping("/getAllPln")
    @Operation(description = "获取全部考核计划")
    public R getIndicator(){
        List<AssessmentPlan> allPlan = assessmentService.getAllPlan();
        return R.success(allPlan);
    }

    @GetMapping("/getPlanEmp")
    @Operation(description = "获取部门考核计划对应的考核人员")
    public R getPlanEmp(@RequestParam(name = "planId") long planId){
        List<Employee> evaluatorByPlanId = assessmentService.getEvaluatorByPlanId(planId);
        System.out.println(evaluatorByPlanId.size());
        System.out.println(evaluatorByPlanId);
        if (evaluatorByPlanId.stream().allMatch(Objects::isNull)) {
            return R.success();
        }
        List<EmpEvaRespVo> list = evaluatorByPlanId.stream().map(employee -> {
            EmpEvaRespVo empEvaRespVo = new EmpEvaRespVo();
            BeanUtils.copyProperties(employee, empEvaRespVo);
            return empEvaRespVo;
        }).toList();
        return R.success(list);
    }

    @GetMapping("/getIndicator")
    @Operation(description = "获取员工对应计划的对应指标")
    public R getIndicator(@RequestParam(name = "planId") long planId){
        AssessmentPlan plan = new AssessmentPlan();
        plan.setPlanId(planId);
        List<AssessmentIndicator> indicatorList = assessmentService.getAssessmentIndicatorList(plan);
        return R.success(indicatorList);
    }

    @PostMapping("/saveScore")
    @Operation(description = "指标结果录入")
    public R updateIndicator(@RequestBody List<AssessmentIndicatorReqEvaVo> indicator){
        System.out.println( indicator);
        List<EmployeeIndicatorScore> indicatorScores = indicator.stream().map(indicatorReqEvaVo -> {
            EmployeeIndicatorScore indicatorScore = new EmployeeIndicatorScore();
            BeanUtils.copyProperties(indicatorReqEvaVo, indicatorScore);
            return indicatorScore;
        }).toList();
        assessmentService.saveScore(indicatorScores);
        return R.success();
    }

    @GetMapping("/getDisputesPlan")
    @Operation(description = "获取异议计划")
    public R getDisputes() {
        List<AssessmentPlan> disputes = assessmentService.getDisputes();
        return R.success(disputes);
    }

    @GetMapping("/getDisputes")
    @Operation(description = "获取异议计划的全部员工")
    public R getDisputesEmpByPlan(long planId) {
        List<Employee> disputes = assessmentService.getDisputesEmp(planId);
        List<EmpEvaRespVo> list = disputes.stream().map(employee -> {
            EmpEvaRespVo empEvaRespVo = new EmpEvaRespVo();
            BeanUtils.copyProperties(employee, empEvaRespVo);
            return empEvaRespVo;
        }).toList();
        return R.success(list);
    }

    @GetMapping("/getDisputesByEmpPlan")
    @Operation(description = "获取员工对应计划的异议")
    public R getDisputesByEmpPlan(@RequestParam("planId") long planId, @RequestParam("empId") long empId) {
        List<AssessmentIndicatorRespVo> indicatorByPId = assessmentService.getIndicatorByPId(planId, empId);
        AssessmentResult assessmentResult = assessmentService.getAssessmentResult(empId, planId);
        List<Disputes> disputesInfo = assessmentService.getDisputesInfo(planId, empId);
        Map<String, Object> map = new HashMap<>();
        map.put("indicators", indicatorByPId);
        map.put("result", assessmentResult);
        map.put("disputes", disputesInfo);
        return R.success(map);
    }

    @PutMapping("/updataDisputesByEmpPlan")
    @Operation(description = "更新员工对计划的异议,同时更新员工指标和计划结果")
    //数据包含，员工编号，计划编号，考核指标编号，考核评语，考核分数
    public R updataDisputesByEmpPlan(@RequestBody DisputesReqVo  disputes) {
        assessmentService.updataDisputesByEmpPlan( disputes);
        return R.success();
    }

}
